package com.simplegovern.authority.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.simplegovern.authority.SysUser;
import com.simplegovern.authority.service.UserService;
import com.simplegovern.base.SimpleGovernAction;

@Scope("prototype")
@Namespace("/user")
@Action("/user/password")
@Results( {   
    @Result(name = "edit", location = "password.jsp")
})
public class PasswordAction extends SimpleGovernAction {
	
	@Autowired
	private UserService userService;
	
	private String oldPassword;
	private String newPassword;
	private String repPassword;
	
	private SysUser user;
	
	public String edit()throws Exception {
		//本为空
		return EDIT;
	}
	
	public String update()throws Exception {
		user.setPassword(newPassword);
		userService.updateSysUser(user);
		addMessage("密码修改成功");
		return null;
	}
	
	public void updateValidator() throws Exception {
		user = userService.getSysUser(getSessionUser().getId());
		if(!user.getPassword().equals(oldPassword)){
			addError("旧密码输入错误");
		}
		if(!newPassword.equals(repPassword)){
			addError("新密码和重复密码必须相等");
		}
		validator.limit(newPassword, 5, 18, "密码必须是5到18个字符");
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public void setRepPassword(String repPassword) {
		this.repPassword = repPassword;
	}
	
	
}
